<template>
  <div class="proStirage">
    <jc-button
      :btn-arr="['reload', 'sub', 'remove', 'option', 'pass', 'batchFill', !list.length && 'check']"
      :disab-arr="['close', 'cancel', 'com2', 'using', 'freeze']"
      :fbillno="fbiVal"
      url="tSubUninstock/querySubUninstockList"
      @audit="audit(1)"
      @noAudit="audit(2)"
      @submit="submit(1)"
      @noSubmit="submit(2)"
      @refresh="handleGetUntreated"
      @del="proAudit"
      @toExcel="handleExport"
      @handleCom1="printing"
      @check="check('up')"
      @check1="check('down')"
    />
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="handleQuerySonClass"
        />
        <el-button
          type="primary"
          class="btn"
          size="mini"
          @click="handleQuerySonClass"
        >
          {{ $t('btn.search') }}
        </el-button>
        <filtration :ftype="ftype" :msg="tacitly" :select-data="selectData" :table-header="tableHeader1" @seachData="handleQuerySonClass" />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        table-selection
        sortable
        get-row-keys="fentryId"
        @selectionChange="handleSelectAll"
        @clickRow="pushDetail"
      >
        <!--        单据编号-->
        <template v-slot:tagSlot="col">
          <el-link type="primary" @click="pushDetail(col.scope.row)">
            {{ col.scope.row.fbillNo }}
          </el-link>
        </template>
        <!--        审核状态-->
        <template v-slot:btnState="col">
          <span v-if="col.scope.row.fdocumentstatus === ''" />
          <el-tag v-else :type="col.scope.row.stateColor">
            {{ col.scope.row.fdocumentstatus }}
          </el-tag>
        </template>
      </jc-table>
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="handleGetUntreated"
      />
    </div>
    <upDown v-if="upDownVis" :msg="cha" :fentryid="fentryData" @client="upDownVis=false" />
  </div>
</template>

<script>
import {
  querySubUninstockList, notsubmitSubUninstock,
  delSubUninstocks, submitSubUninstock,
  unauditSubUninstocks, auditSubUninstocks
} from '@/api/production/productionList'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import { toMxBmina } from '@/components/ToMxamineState'
import filtration from '@/components/filtration'
import jcButton from '@/components/Button'

export default {
  name: 'ProSupplement',
  components: {
    upDown: () => import('@/components/upDown'),
    search,
    jcButton,
    filtration
  },
  mixins: [searData],
  props: {
    list: {
      type: Array,
      default: function() {
        return []
      }
    }
  },
  data() {
    return {
      upDownVis: false,
      cha: '',
      fentryData: [],
      ftype: 25,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      proData: [],
      tableHeight: '70vh',
      fbiVal: '',
      // 表头
      tableData: [], // 生产领料数据
      tableHeader: [
        { label: this.$t('market.rkdh'), type: 'tag', prop: 'fbillNo', width: '130px' },
        { label: this.$t('market.orderDate'), prop: 'fdate', width: '80' },
        { label: this.$t('market.documentstatus'), type: 'state', prop: 'fdocumentStatus', width: '70' },
        { label: this.$t('market.fmodel'), prop: 'fmodel', width: '100' },
        { label: this.$t('market.fmaterial'), prop: 'fnumber', width: '110px' },
        { label: this.$t('shop.fname2'), prop: 'fname' },
        { label: this.$t('market.supplier'), prop: 'fsupplierName' },
        { label: this.$t('market.specificaTion'), prop: 'fspecificaTion' },
        { label: this.$t('market.fdescripTion'), prop: 'fdescripTion', minWidth: '200px' },
        { label: this.$t('market.funit'), prop: 'funitName', width: '80' },
        { label: this.$t('market.appQty'), prop: 'fmustQty', width: '80' },
        { label: this.$t('market.realQty'), prop: 'frealQty', width: '80' },
        { label: this.$t('market.stock2'), prop: 'fstockName' },
        { label: this.$t('market.salKhddh'), prop: 'salKhddh' },
        { label: this.$t('market.mobillNo'), prop: 'fmobillNo' },
        { label: this.$t('market.pbomBillNo'), prop: 'ppbomBillNo' }
      ],
      tableHeader1: []
    }
  },
  created() {
    this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 230px)' : '73vh'
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else if (this.val && this.val.length > 0) {
          const list = this.val.map(item => {
            return toMxBmina(item)
          })
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '委外退库列表')
        } else {
          querySubUninstockList({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.list.map(item => {
                return toMxBmina(item)
              })
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '委外退库列表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取委外退库列表数据
    async handleGetUntreated(ev) {
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        pageNum: this.currentPage,
        pageSize: this.size,
        search: this.searCollData.concat(this.details)
      }
      await querySubUninstockList(DATA).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.list
          this.tableData.map(item => {
            return toMxBmina(item)
          })
          this.total = res.data.total
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    // 详情
    pushDetail(ev) {
      const id = ev.fid
      this.$router.push({
        path: `/outTuiStockDetail/${id}`
      })
    },
    // 多选
    handleSelectAll(val) {
      this.fbiVal = val[0].fbillNo
      this.selectVal = []
      this.fentryData = []
      this.val = val
      val.forEach(res => {
        res.fbillNo && this.selectVal.push(res.fbillNo)
        this.fentryData.push(res.fentryId)
      })
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: this.selectVal }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.handleGetUntreated()
        this.selectVal = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 提交
    async submit(ev) {
      const api = ev === 1 ? submitSubUninstock : notsubmitSubUninstock
      this.toExamineFun(api)
    },
    // 审核
    async audit(ev) {
      const api = ev === 1 ? auditSubUninstocks : unauditSubUninstocks
      this.toExamineFun(api)
    },
    // 删除
    proAudit() {
      this.$confirm('确定要删除该订单么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const api = delSubUninstocks
        this.toExamineFun(api)
      })
    },
    // 上查、下查
    check(ev) {
      if (this.fentryData.length === 0) return
      this.cha = ev
      this.upDownVis = true
    },
    // 套打
    printing() {
      const id = this.fbiVal
      this.$router.push({
        path: `/proStirTong/${id}`
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.proStirage {
  @include listBom;

  .header {
    position: relative;
    .header-name {
      width: 100%;
      .input-width {
        width: 100px;
      }
    }
    .btn {
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
